import symbolPkg from './symbol.js'
var LGXSymbol = symbolPkg.LGXSymbol;
import UtilFuncs from '../common/tools/utilfuncs.js'
var utilfuncs = UtilFuncs.UtilFuncs;

import GlobalConst from '../common/globalConst.js'
var global = GlobalConst.GlobalConst;
import TerminalPkg from '../common/struct/basestruct/terminal.js'

class TDevContainTermInfo {
    constructor(type) {
        this.devIconTypeID = type;
        this.terminalLst = new Array();
    }
    addTerminal(p) {
        this.terminalLst.push(p);
    }
}

class SymbolManger {
    constructor() {
        this.m_tSymbolList = new Array();
        this.m_tSymbolTermMap = new Map();
    }

    getAllSymbol() {
        return this.m_tSymbolList;
    }

    initSymbol2Mem(tAllSymbolUnitLst, tAllSymbolDefLst) {
        this.clearExSymbols();
        let len = tAllSymbolUnitLst.length;
        for (let i = 0; i < len; i++) {
            let obj = tAllSymbolUnitLst[i];
            this.addSymbolInfo(obj);
        }
        this.endInitSymbol(tAllSymbolDefLst);
    }

    addSymbolInfo(symbolInfo) {
        let symTypeID = symbolInfo.superID;
        if (utilfuncs.mainObjType(symbolInfo.type) == global.TerminalType) {
            console.log();
        }
        var pSymbol = this.getSymbol(symbolInfo.superID, symbolInfo.state);
        if (!pSymbol) {
            pSymbol = this.createNewSymbol(symbolInfo);
        }
        if (utilfuncs.mainObjType(symbolInfo.type) == global.TerminalType) {
            if (pSymbol) {
                pSymbol.addSymbolUnit(symbolInfo);
                this.addSymbolTermInfo(symbolInfo);
            }
            else {
                console.log();
            }
        }
        else {
            pSymbol.addSymbolUnit(symbolInfo);
        }
    }

    getSymbol(symUUID, state) {
        let destSymbol = null;
        let len = this.m_tSymbolList.length;
        for (let i = 0; i < len; i++) {
            let symbol = this.m_tSymbolList[i];
            if (symbol.getUUID() == symUUID && symbol.getState() == state) {
                destSymbol = symbol;
                break;
            }
        }
        return destSymbol;
    }

    getSymbolByType(type, state) {
        let destSymbol = null;
        let len = this.m_tSymbolList.length;
        for (let i = 0; i < len; i++) {
            let symbol = this.m_tSymbolList[i];
            if (symbol.getTypeID() == type && symbol.getState() == state) {
                destSymbol = symbol;
                break;
            }
        }
        return destSymbol;
    }


    createNewSymbol(symbolInfo) {
        let pSymbol = null;
        let symTypeID = symbolInfo.symbolType;
        let symUUID = symbolInfo.superID;
        if (utilfuncs.mainObjType(symbolInfo.type) != global.TerminalType) {
            pSymbol = this.getSymbol(symTypeID, symbolInfo.state);
            if (pSymbol == null) {
                pSymbol = new LGXSymbol(symTypeID, symUUID,symbolInfo.state);
                pSymbol.exflag = symbolInfo.exflag;
                if  (symbolInfo.exflag === undefined){
                    console.log(symbolInfo.sSymbolName);
                }
                this.m_tSymbolList.push(pSymbol);
            }
        }
        return pSymbol;
    }

    endInitSymbol(tAllSymbolDefLst) {
        let len = this.m_tSymbolList.length;
        for (let i = 0; i < len; i++) {
            let symbolObj = this.m_tSymbolList[i];
            let dstSmbolDef = this.findSymbolDef(symbolObj.getUUID(), tAllSymbolDefLst);
            if (dstSmbolDef && dstSmbolDef.name.length > 0) {
                symbolObj.setName(dstSmbolDef.name);
                symbolObj.setMoldActionType(dstSmbolDef.moldActionType);
            }
            this.setDevSymbolTerminalInfo(symbolObj);
            symbolObj.endInitSymbol();
        }
        this.asignBlockUnitRefSymbol();//为图符中子图元也是图符的设置关联图符指针
    }

    //为图符中子图元也是图符的设置关联图符指针
    asignBlockUnitRefSymbol() {
        let len = this.m_tSymbolList.length;
        for (let i = 0; i < len; i++) {
            let symbolObj = this.m_tSymbolList[i];
            symbolObj.asignBlockUnitRefSymbol(this);
        }
    }

    findSymbolDef(symTypeID, tAllSymbolDefLst) {
        let dstSmbolDef = null;
        for (let i = 0; i < tAllSymbolDefLst.length; i++) {
            let obj = tAllSymbolDefLst[i];
            if (obj.type == symTypeID) {
                dstSmbolDef = obj;
                break;
            }
        }
        return dstSmbolDef;
    }

    findSymbolTermInfo(type) {
        var pinfo = null;
        var iterator = this.m_tSymbolTermMap.keys();
        for (let key of iterator) {
            var value = this.m_tSymbolTermMap.get(key);
            if (key == type) {
                pinfo = value;
                break;
            }
        }
        return pinfo;
    }
    addSymbolTermInfo(terminalInfo) {
        var devTypeID = terminalInfo.superID;
        var pInfo = this.findSymbolTermInfo(devTypeID);
        if (!pInfo) {
            pInfo = new TDevContainTermInfo(devTypeID);
            this.m_tSymbolTermMap.set(devTypeID, pInfo);
        }
        else {
            console.log();
        }
        pInfo.addTerminal(terminalInfo);
    }

    setDevSymbolTerminalInfo(devSymbol) {
        var devTypeID = devSymbol.getUUID();
        var pInfo = this.findSymbolTermInfo(devTypeID);
        if (pInfo) {
            var havaTerminal = false;
            let len = devSymbol.m_GraphUnitLst.length;
            for (let i = 0; i < len; i++) {
                let temp = devSymbol.m_GraphUnitLst[i];
                var f = temp instanceof TerminalPkg.Terminal;
                if (f) {
                    havaTerminal = true;
                    break;
                }
            }
            if (!havaTerminal) {
                let len = pInfo.terminalLst.length;
                for (let i = 0; i < len; i++) {
                    let temp = pInfo.terminalLst[i];
                    f = temp instanceof TerminalPkg.Terminal;
                    if (f) {
                        devSymbol.addSymbolUnit(temp);
                    }
                }

            }
        }
        else {
            console.log();
        }
    }

    getSymbolStateCount(typeID) {
        let count = 0;
        let len = this.m_tSymbolList.length;
        for (let i = 0; i < len; i++) {
            let symbol = this.m_tSymbolList[i];
            if (symbol.getUUID() == typeID) {
                count++;
            }
        }
        return count;
    }

    clearExSymbols() {
        let len = this.m_tSymbolList.length;
        for (let i = len - 1; i >= 0; i--) {
            let symbol = this.m_tSymbolList[i];
            if (symbol.exflag) {
                this.m_tSymbolList.splice(i, 1);
            }
            else{
                symbol.setBeenRefFlag(false);
            }
        }
    }
}

export default { SymbolManger }